package com.molichuxing.gateway.api.services.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.gateway.api.services.VehicleApiService;
import com.molichuxing.gateway.bff.basic.entity.response.VehicleModlemakeVo;
import com.molichuxing.gateway.bff.car.convertor.Convert;
import com.molichuxing.gateway.bff.car.entity.request.modify.PdiCheckModifyVo;
import com.molichuxing.gateway.bff.car.entity.response.*;
import com.molichuxing.gateway.bff.car.services.*;
import com.molichuxing.gateway.utils.RequestAttrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: renzhengyu
 * @Description:
 * @Date: Created in 14:46 2019/9/11
 * @Modified By:
 **/
@Api(tags = "车辆相关接口")
@RestController
@RequestMapping(value = "/vehicle")
public class VehicleApiServiceImpl implements VehicleApiService {
    private static final Logger logger = LoggerFactory.getLogger(VehicleApiServiceImpl.class);


    @Resource
    private ViolationBffService violationBffService;

    @Resource
    private AccidentBffService accidentBffService;

    @Resource
    private CarBffService carBffService;

    @Resource
    private InsuranceBffService insuranceBffService;

    @Resource
    private CarCheckBffService carCheckBffService;

    @Resource
    private MaintenanceBffService maintenanceBffService;

    @Override
    @ApiOperation(value = "获取车辆违章列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "licencePlate", value = "车牌号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "vin", value = "车架号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "dealerId", value = "经销商id", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "userName", value = "用户名", required = false, dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "status", value = "违章处理状态(0:全部，1:未处理未交费, 2:已处理未交费, 3:已处理已交费)", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")

    })
    @GetMapping(value = "/violation/list")
    public ResponseResult<Paged<CarsViolationVo>> getCarsViolationPaged(@RequestParam Map<String, Object> params,@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        ResponseResult<Paged<CarsViolationVo>> result = new ResponseResult<>();
        try {
            result.success(violationBffService.getCarsViolationPaged(params, pageNum, pageSize));
        } catch (Exception e) {
            // 记录异常，返回异常信息
            logger.error("[orderDealerList]获取经销商订单列表异常", e);
            result.error("获取车辆违章列表异常:" + e.getMessage());
        }
        return result;
    }

    @Override
    @ApiOperation(value = "获取事故列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "licencePlate", value = "车牌号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "vin", value = "车架号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "userName", value = "用户", required = false, dataType = "String"),
            @ApiImplicitParam(name = "liabilityType", value = "责任类型（0：无责，1：全责，2：主责，3：同责，4：次责）", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "isCasualty", value = "是否有人伤（0：无人伤，1：有人伤）", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "accidentType", value = "事故类型（1:单方事故，2：双方事故，3：多方事故，4：停放受损，5：车辆盗抢，6：车辆自燃，7：车辆涉水，8：重大自然灾害）", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "accidentStartTime", value = "出险开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "accidentEndTime", value = "出险结束时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "status", value = "事故状态（空：全部，1：事故未确认，2：事故已确认，3：结案未确认，4：已结案，5：无效报备）", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "sortName", value = "排序字段", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sortWay", value = "排序方式 asc:正序，desc:倒叙", required = false, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")

    })
    @GetMapping(value = "/accident/list")
    public ResponseResult<Paged<AccidentBizVo>> getCarAccidentPage(@RequestParam Map<String, Object> map, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        ResponseResult<Paged<AccidentBizVo>> result = new ResponseResult<>();
        try {
            //获取当前登录的经销商id
            Integer dealerId = RequestAttrUtil.getCurrentDealerId();
            map.put("dealerId", dealerId);
            return result.success(accidentBffService.getCarAccidentPage(map, pageNum, pageSize));
        } catch (Exception e) {
            logger.error("[accident/list]获取事故列表信息异常", e);
            result.error("获取事故列表信息异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "获取事故详细信息")
    @ApiImplicitParam(name = "accidentId", value = "事故id", required = true, dataType = "Integer")
    @GetMapping(value = "/accident/detail")
    public ResponseResult<AccidentDetailVo> getAccident(@RequestParam Integer accidentId) throws Exception {
        ResponseResult<AccidentDetailVo> result = new ResponseResult<>();
        try {
            return result.success(accidentBffService.getAccident(accidentId));
        } catch (Exception e) {
            logger.error("[accident/detail]获取事故详细信息异常", e);
            result.error("获取事故详细信息异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "根据车辆id获取车辆详细信息（事故）")
    @ApiImplicitParam(name = "carId", value = "车辆id", required = true, dataType = "Integer")
    @GetMapping(value = "/cars/detail/accident")
    public ResponseResult<List<AccidentBizVo>> getCarAccidentList(@RequestParam Integer carId) {
        ResponseResult<List<AccidentBizVo>> result = new ResponseResult<>();
        try {
            return result.success(accidentBffService.getCarAccidentList(carId));
        } catch (Exception e) {
            logger.error("[cars/detail]获取车辆详细信息异常", e);
            result.error("获取车辆详细信息异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "根据车辆id获取车辆详细信息（交强险，商业险）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carId", value = "车辆id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "type", value = "类型1：交强险，2：商业险", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/cars/detail/insurance")
    public ResponseResult<List<CarInsuranceVo>> getInsuranceList(@RequestParam Integer carId, @RequestParam Integer type) {
        ResponseResult<List<CarInsuranceVo>> result = new ResponseResult<>();
        try {
            return result.success(insuranceBffService.getInsuranceList(carId, type));
        } catch (Exception e) {
            logger.error("[cars/detail]获取车辆详细信息异常", e);
            result.error("获取车辆详细信息异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "根据车辆id获取车辆详细信息（检验）")
    @ApiImplicitParam(name = "carId", value = "车辆id", required = true, dataType = "Integer")
    @GetMapping(value = "/cars/detail/check")
    public ResponseResult<List<CarCheckVo>> getCarCheckListByCarId(Integer carId) {
        ResponseResult<List<CarCheckVo>> result = new ResponseResult<>();
        try {
            return result.success(carCheckBffService.getCarCheckListByCarId(carId));
        } catch (Exception e) {
            logger.error("[cars/detail]获取车辆详细信息异常", e);
            result.error("获取车辆详细信息异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "根据车辆id获取车辆详细信息（违章）")
    @ApiImplicitParam(name = "carId", value = "车辆id", required = true, dataType = "Integer")
    @GetMapping(value = "/cars/detail/violation")
    public ResponseResult<List<CarsViolationVo>> getViolationListByCarId(Integer carId) {
        ResponseResult<List<CarsViolationVo>> result = new ResponseResult<>();
        try {
            return result.success(violationBffService.getCarsViolationByCarId(carId));
        } catch (Exception e) {
            logger.error("[cars/detail]获取车辆详细信息异常", e);
            result.error("获取车辆详细信息异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "根据车辆id获取车辆详细信息（保养）")
    @ApiImplicitParam(name = "carId", value = "车辆id", required = true, dataType = "Integer")
    @GetMapping(value = "/cars/detail/maintenance")
    public ResponseResult<List<MaintenanceRecordVo>> getMaintenanceByCarId(Integer carId) {
        ResponseResult<List<MaintenanceRecordVo>> result = new ResponseResult<>();
        try {
            return result.success(maintenanceBffService.getMaintenanceByCarId(carId));
        } catch (Exception e) {
            logger.error("[cars/detail]根据车辆id获取车辆详细信息（保养）", e);
            result.error("根据车辆id获取车辆详细信息（保养）");
        }
        return result;
    }


    @Override
    @ApiOperation(value = "获取车辆信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "licencePlate", value = "车牌", required = false, dataType = "String"),
            @ApiImplicitParam(name = "vin", value = "车架号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "vehicleId", value = "车型Id", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "stockId", value = "门店id", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "vehicleStatus", value = "空：全部， 0：待出租，1：已出租，2：不可租", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "sortName", value = "排序字段", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sortWay", value = "排序方式 asc:正序，desc:倒叙", required = false, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/cars/list")
    public ResponseResult<Paged<CarVo>> getCarList(@RequestParam Map<String, Object> selectParams, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        ResponseResult<Paged<CarVo>> result = new ResponseResult<>();
        try {
            return result.success(carBffService.getCarList(selectParams, pageNum, pageSize));
        } catch (Exception e) {
            logger.error("[cars/list]获取车辆列表信息异常", e);
            result.error("获取车辆列表信息异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "获取车辆已有的品牌")
    @GetMapping(value = "/cars/have/make")
    public ResponseResult<List<VehicleModlemakeVo>> getVehicleMakeByVehicle() {
        ResponseResult<List<VehicleModlemakeVo>> result = new ResponseResult<>();
        try {
            return result.success(carBffService.getVehicleMakeByVehicle());
        } catch (Exception e) {
            logger.error("[cars/list]获取车辆列表信息异常", e);
            result.error("获取车辆列表信息异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "获取车辆轨迹的时间范围")
    @ApiImplicitParam(name = "vin", value = "车架号", required = true, dataType = "String")
    @GetMapping(value = "/trajectory/time")
    public ResponseResult<List<String>> getTrajectoryTime(@RequestParam String vin) {
        ResponseResult<List<String>> result = new ResponseResult<>();
        try {
            return carBffService.getTrajectoryTime(vin);
        } catch (Exception e) {
            logger.error("[getTrajectoryTime]获取车辆轨迹的时间范围", e);
            result.error("获取车辆轨迹的时间范围");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "获取车辆轨迹")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vin", value = "车架号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间 格式：2019-01-07 15:55:51", required = true, dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间 格式：2019-01-07 15:55:51", required = true, dataType = "String")
    })
    @GetMapping(value = "/trajectory/record")
    public ResponseResult<List<IovTrajectoryVo>> getTrajectory(@RequestParam String vin, @RequestParam String startTime, @RequestParam String endTime) {
        ResponseResult<List<IovTrajectoryVo>> result = new ResponseResult<>();
        try {
            return result.success(carBffService.getTrajectory(vin, startTime, endTime));
        } catch (Exception e) {
            logger.error("[getTrajectoryTime]获取车辆轨迹异常", e);
            result.error("获取车辆轨迹异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "获取车辆详细信息")
    @ApiImplicitParam(name = "carId", value = "车辆id", required = true, dataType = "Integer")
    @GetMapping(value = "/cars/detail")
    public ResponseResult<CarDetailVo> getCarDetail(@RequestParam Integer carId) {
        ResponseResult<CarDetailVo> result = new ResponseResult<>();
        try {
            return result.success(carBffService.getCarDetail(carId));
        } catch (Exception e) {
            logger.error("[cars/detail]获取车辆详细信息异常", e);
            result.error("获取车辆详细信息异常");
        }
        return result;
    }


    @Override
    @ApiOperation(value = "获取PDI检验列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "licencePlate", value = "车牌号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "vin", value = "车架号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "orderCode", value = "订单号", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "status", value = "检验状态(0:全部,1：待检验,2：待用户确认,3：待人工介入,4：已检验)", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/check/pdilist")
    public ResponseResult<Paged<CarCheckVo>> getPdiCarCheck(@RequestParam Map<String, Object> selectParams) {
        ResponseResult<Paged<CarCheckVo>> result = new ResponseResult<>();
        try {
            //获取当前登录的经销商id
            Integer dealerId = RequestAttrUtil.getCurrentDealerId();
            selectParams.put("dealerId", dealerId);
            return result.success(carCheckBffService.getPdiCarCheck(selectParams));
        } catch (Exception e) {
            logger.error("[getPdiCarCheck]获取PDI检验列表信息异常", e);
            result.error("获取PDI检验列表信息异常");
        }
        return result;
    }

    /**
     * 获取检验详情基础信息
     *
     * @param checkId 检验详情基础信息
     * @return
     */
    @Override
    @ApiOperation(value = "获取检验详情基础信息")
    @ApiImplicitParam(name = "checkId", value = "检验id", required = true, dataType = "Integer")
    @GetMapping(value = "/check/base")
    public ResponseResult<CarBasicVo> getCheckBasicInfo(@RequestParam Integer checkId) {
        ResponseResult<CarBasicVo> result = new ResponseResult<>();
        try {
            return result.success(carCheckBffService.getCheckBasicInfo(checkId));
        } catch (Exception e) {
            logger.error("[getCheckBasicInfo]获取检验详情基础信息异常", e);
            result.error("获取检验详情基础信息异常");
        }
        return result;
    }

    /**
     * 根据检验id 获取检验信息
     *
     * @param
     * @return
     */
    @Override
    @ApiOperation(value = "根据检验id获取PDI检验信息")
    @ApiImplicitParam(name = "checkId", value = "检验id", required = true, dataType = "Integer")
    @GetMapping(value = "/check/pdicheckdetail")
    public ResponseResult<PdiCheckVo> getPdiCheckNewInfo(@RequestParam Integer checkId) {
        ResponseResult<PdiCheckVo> result = new ResponseResult<>();
        try {
            return result.success(carCheckBffService.getPdiCheckNewInfo(checkId));
        } catch (BizException e) {
            logger.error("[pdicheckcar]pdi检验异常异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[getPdiCheckNewInfo]根据检验id获取PDI检验信息异常", e);
            result.error("根据检验id获取PDI检验信息");
        }
        return result;
    }


    @Override
    @ApiOperation(value = "PDI检验-上传检验报告")
    @PostMapping(value = "/check/pdicheckcar")
    public ResponseResult<Boolean> pdiCheckCar(@RequestBody PdiCheckModifyVo pdiCheckModifyVo) throws Exception {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            return result.success(carCheckBffService.pdiCheckCar(Convert.toPdiCheckModifyDto(pdiCheckModifyVo)));
        } catch (Exception e) {
            logger.error("[pdicheckcar]PDI检验-上传检验报告异常", e);
            result.error("上传检验报告异常");
        }
        return result;
    }
}
